package com.whcoding.test.controller;

import com.whcoding.test.common.api.CommonPage;
import com.whcoding.test.common.api.CommonResult;
import com.whcoding.test.pojo.OrmCustomerVO;
import com.whcoding.test.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Max;
import java.util.List;

/**
 * @program: spring-boot-learning
 * @description:
 * @author: whcoding
 * @create: 2022-04-18 16:06
 **/

@RestController
@Validated
@RequestMapping("/ormCustomer")
public class CustomerController {

	@Autowired
	CustomerService customerService;

	/**
	 * 添加log信息
	 */
	private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);

	/**
	 * 获取所有客户列表
	 *
	 * @return
	 */
	@GetMapping(value = "/findBrandList")
	@ResponseBody
	public CommonResult<List<OrmCustomerVO>> findCustomerList() {
		return CommonResult.success(customerService.getCustomerList());
	}

	/**
	 * 分页查询数据
	 */
	@GetMapping(value = "/list")
	@ResponseBody
	public CommonResult<CommonPage<OrmCustomerVO>> listCustomer(Integer pageNum, Integer pageSize) {
		List<OrmCustomerVO> brandList = customerService.listCustomerByPage(pageNum, pageSize);
		return CommonResult.success(CommonPage.restPage(brandList));
	}


	/**
	 * 根据id查询数据
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/{id}")
	@ResponseBody
	public CommonResult<OrmCustomerVO> customerById(@Valid @PathVariable("id") @Max(value = 5, message = "超过 id 的范围了") Long id) {
		return CommonResult.success(customerService.findCustomerByIds(id));
	}


	/**
	 * 根据id查询数据
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/ById/{id}")
	@ResponseBody
	public CommonResult<OrmCustomerVO> customerById2(@Valid @PathVariable("id") @Max(value = 5, message = "超过 id 的范围了") Integer id) {
		return CommonResult.success(customerService.getCustomerByID(id));
	}



	/**
	 * 添加客户
	 *
	 * @param customerVO
	 * @return
	 */
	@PostMapping(value = "/save")
	@ResponseBody
	public CommonResult saveCustomer(@RequestBody OrmCustomerVO customerVO) {
		CommonResult commonResult;
		int count = customerService.saveCustomer(customerVO);
		if (count == 1) {
			commonResult = CommonResult.success(customerVO);
			logger.debug("createBrand success:{}", customerVO);
		} else {
			commonResult = CommonResult.failed("操作失败");
			logger.debug("createBrand failed:{}", customerVO);
		}
		return commonResult;
	}


	/**
	 * 更新指定id 客户信息
	 */
	@PostMapping(value = "/update/{id}")
	@ResponseBody
	public CommonResult updateCustomer(@PathVariable("id") Long id, @RequestBody OrmCustomerVO customerVO) {
		CommonResult commonResult;
		int count = customerService.updateCustomer(id, customerVO);
		if (count == 1) {
			commonResult = CommonResult.success(customerVO);
			logger.debug("updateBrand success:{}", customerVO);
		} else {
			commonResult = CommonResult.failed("操作失败");
			logger.debug("updateBrand failed:{}", customerVO);
		}
		return commonResult;
	}


	/**
	 * 根据ID 删除数据
	 */
	@GetMapping(value = "/delete/{id}")
	@ResponseBody
	public CommonResult deleteCustomer(@PathVariable("id") Long id) {
		int count = customerService.deleteCustomerId(id);
		if (count == 1) {
			logger.debug("deleteBrand success :id={}", id);
			return CommonResult.success(null);
		} else {
			logger.debug("deleteBrand failed :id={}", id);
			return CommonResult.failed("操作失败");
		}
	}



}